/**
 * 
 */
package com.yidin.lsc;

import java.util.ArrayList;
import java.util.List;

import com.yidin.util.AspLog;
import com.yidin.widget.Carema;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 主界面，即导航页面
 * @author cola
 * 
 */
public class NavigationPage extends Views {
	private List<Navigation> netInfos = null;
	private ListView gameList;
	private View contentView;
	private NavigationAdapter nNavigationAdapter;
	private Activity activity;
	private ImageView rightIV;
	private Button photoBtn;
	private Button settingBtn;
	private Button notificationBtn;
	private lscManager mylscManager;
	private int rightIVWidth = 0;

	public NavigationPage(Activity activity) {
		super(activity);
		// TODO Auto-generated constructor stub
		this.activity = activity;
		// this.checkCache();
		this.mylscManager = lscManager.getInstance();
		initialize();
		
	}

	public void findView() {
		this.gameList = (ListView) contentView
				.findViewById(R.id.mygame_ListView);
		this.rightIV = (ImageView) contentView.findViewById(R.id.rightIV);
		this.photoBtn = (Button) contentView.findViewById(R.id.navigationPhoto);
		this.settingBtn = (Button) contentView.findViewById(R.id.setting);
		this.notificationBtn = (Button) contentView.findViewById(R.id.nav_noti);
	}

	public void initialize() {
		this.netInfos = new ArrayList<Navigation>();

		Navigation n8 = new Navigation();
		n8.title = "";
		n8.icon = R.drawable.logo;
		n8.content = "个人主页";
		this.netInfos.add(n8);

		Navigation n9 = new Navigation();
		n9.title = "朋友圈";
		n9.icon = R.drawable.icon1;
		n9.content = "附近的";
		this.netInfos.add(n9);

		Navigation n2 = new Navigation();
		n2.title = "";
		n2.icon = R.drawable.icon2;
		n2.content = "我的好友";
		this.netInfos.add(n2);

		Navigation n1 = new Navigation();
		n1.title = "";
		n1.icon = R.drawable.icon3;
		n1.content = "大小圈相亲";
		this.netInfos.add(n1);

		Navigation n3 = new Navigation();
		n3.title = "优惠圈";
		n3.icon = R.drawable.icon4;
		n3.content = "附近商圈";
		this.netInfos.add(n3);

		Navigation n4 = new Navigation();
		n4.title = "";
		n4.icon = R.drawable.icon5;
		n4.content = "优惠券";
		this.netInfos.add(n4);

		Navigation n5 = new Navigation();
		n5.title = "";
		n5.icon = R.drawable.icon6;
		n5.content = "团购";
		this.netInfos.add(n5);

		Navigation n6 = new Navigation();
		n6.title = "";
		n6.icon = R.drawable.icon7;
		n6.content = "会员卡";
		this.netInfos.add(n6);

		Navigation n7 = new Navigation();
		n7.title = "活动圈";
		n7.icon = R.drawable.icon8;
		n7.content = "热门活动";
		this.netInfos.add(n7);

		this.nNavigationAdapter = new NavigationAdapter(this.activity,
				this.netInfos);
		this.gameList.setAdapter(nNavigationAdapter);
		this.gameList.setOnItemClickListener(itemClickListener);
		this.photoBtn.setOnClickListener(photoOnClickListener);
		this.settingBtn.setOnClickListener(settingOnClickListener);
		this.notificationBtn.setOnClickListener(notificationOnClickListener);
	}

	@Override
	public void checkCache() {
		// TODO Auto-generated method stub

	}

	public class Navigation {
		/**
		 * 栏目标题
		 */
		public String title;
		/**
		 * 功能
		 */
		public String content;
		public int icon;
	}

	@Override
	public void showFunctionsListView() {
		// TODO Auto-generated method stub
		contentView = addView(R.layout.navigationpage);
	}

	public void setViewGone(){
		rightIV.setVisibility(View.GONE);
	}
	
	/**
	 * 获取页面截图
	 * 
	 * @param activity
	 * @return
	 */
	public static Bitmap takeScreenShot(Activity activity, int imageWidth) {
		// View是你需要截图的View
		View view = LscActivity.self.views[1].getActiveView();// activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap b1 = view.getDrawingCache();
		if(b1 == null) return null;
		
		int s = b1.getHeight();

		// 获取状态栏高度
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		System.out.println(statusBarHeight);
		// 获取屏幕长和高
		int width = imageWidth;
		int height = activity.getWindowManager().getDefaultDisplay()
				.getHeight();
		// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
		// 去掉标题栏
		Bitmap b = Bitmap.createBitmap(b1, 0, 0, width, s);
		view.destroyDrawingCache();
		s = b.getHeight();
		return b;
	}

	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {// (activity.getResources().getDrawable((R.drawable.login)))
			rightIV.setImageBitmap(takeScreenShot(activity, getImageWidth()));//
			mylscManager.pager.setCurrentItem(1);
			switch (arg2) {
			case 0:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.dynamic);
				break;
			case 1:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.nearby);
				break;
			case 2:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.nearby);
			case 3:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.nearby);
				break;
			case 4:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.nearby);
				break;
			case 5:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.coupon);
				break;
			case 6:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.storemainmenu);
				break;
			case 7:
				mylscManager.pagerControl.getViews(1).setCurrionPage(arg2,
						R.layout.storemainmenu);
				break;
			}

		}
	};

	public void destoryIV() {

		if (rightBM != null)
			rightBM.recycle();
	}

	private Bitmap rightBM;

	public int getImageWidth() {
		if (rightIVWidth == 0)
			rightIVWidth = rightIV.getWidth();
		return rightIVWidth;
	}

	public void setRightImage(Bitmap bm) {
		if (bm != null)
			rightIV.setImageBitmap(bm);
	}

	public static final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				;
				break;

			}
		}
	};

	private OnClickListener photoOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, Carema.class);
			activity.startActivity(it);
		}

	};

	private OnClickListener notificationOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, NotificationCenter.class);
			activity.startActivity(it);
			// contentView = addView(R.layout.myseting);
			// ((LinearLayout)
			// contentView.findViewById(R.id.outRightBtn)).setVisibility(View.GONE);
			// ((TextView)
			// contentView.findViewById(R.id.title)).setText("我的设置");
		}

	};
	
	private OnClickListener settingOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent it = new Intent(activity, MySetting.class);
			activity.startActivity(it);
			// contentView = addView(R.layout.myseting);
			// ((LinearLayout)
			// contentView.findViewById(R.id.outRightBtn)).setVisibility(View.GONE);
			// ((TextView)
			// contentView.findViewById(R.id.title)).setText("我的设置");
		}

	};

	// 获取当前进程的RunningTaskInfo
	public static synchronized RunningTaskInfo getCurrentTask(Context context,
			Class<?> rootclass) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Service.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = am.getRunningTasks(10);
		if (tasks == null || tasks.isEmpty()) {
			// AspLog.w(TAG, "RunningTaskInfo list is null or empty, reload!");
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// tasks = PackageUtil.getRunningTasks(100);//
			// am.getRunningTasks(100);
		}
		if (tasks == null || tasks.isEmpty()) {
			// AspLog.w(TAG,
			// "RunningTaskInfo list is null or empty, create self RunningTaskInfo!");
			RunningTaskInfo t = new RunningTaskInfo();
			t.baseActivity = new ComponentName(context, rootclass);
			t.numActivities = 1;
			t.topActivity = new ComponentName(context, rootclass);
			t.numRunning = 1;
			return t;
		}
		RunningTaskInfo info = tasks.get(0);
		for (RunningTaskInfo t : tasks) {
			if (isMyTask(context, t)) {
				AspLog.v("getCurrentTask", "getCurrentTask: " + t.topActivity);
				return t;
			}
		}
		// AspLog.v(TAG, "getCurrentTask2: " + info.topActivity);
		return info;
	}

	public static synchronized boolean isMyTask(Context context,
			RunningTaskInfo task) {
		if (task == null || context == null)
			return false;
		String packagename = context.getPackageName();
		if (packagename != null
				&& (packagename.equals(task.baseActivity.getPackageName()) || packagename
						.equals(task.topActivity.getPackageName()))) { // Michael
																		// Liu:2012.2.20针对第三方应用增加
			return true;
		} else
			return false;
	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		
	}
}
